package com.richpay.landiprintdemo.print;

import android.content.Context;

import com.richpay.landiprintdemo.data.TransData;


public class LanDiPrinter {
    private static LanDiPrinter lanDiPrinter = null;
    private PrinterImpl printerInpl;
    private Context context;

    private LanDiPrinter(Context context) {
        this.context = context;
    }

    /**
     * 单例
     *
     * @return 返回一个 打印工具
     */
    public static LanDiPrinter getInstance(Context context) {
        if (lanDiPrinter == null) {
            synchronized (PrinterImpl.class) {
                if (lanDiPrinter == null) {
                    lanDiPrinter = new LanDiPrinter(context);
                }
            }
        }
        return lanDiPrinter;
    }

    /**
     * @param transData 打印小票
     */
    public void printTicket(TransData transData, int type) {
        printerInpl = new PrinterImpl(context);
        printerInpl.init();
        printerInpl.setListener(listener);
        printerInpl.addText(transData, type, false);
        printerInpl.startPrint();
    }


    /**
     * @param transData 重打印
     */
    public void rePrintTicket(TransData transData, int type) {
        printerInpl = new PrinterImpl(context);
        printerInpl.init();
        printerInpl.setListener(listener);
        printerInpl.addText(transData, type, true);
        printerInpl.startPrint();
    }

    /**
     * 打印监听
     */
    public interface PrinterListener {
        void onPrintStart();

        void onFinish();

        void onError(String s);
    }

    public PrinterListener listener;

    public PrinterListener getListener() {
        return listener;
    }

    public void setListener(PrinterListener listener) {
        this.listener = listener;
    }


}
